<template>
  <div
    class="banner-wrapper" 
    :style="{background: `url(${bgImage}) center center/6000px`}"
  >
    <div class="banner wrap-v2">
      <div class="banner-left">
        <a-carousel 
          effect="fade"
          autoplay
          ref="bannerRef"
          :before-change="bannerBeforeChange"
        >
          <div 
            class="banner-item"
            v-for="item in topBanners"
            :key="item.imageUrl"  
          >
            <img 
              class="image" 
              :src="item.imageUrl" 
              :alt="item.typeTitle"
            >
          </div>
        </a-carousel>
      </div>

      <div class="banner-right">
        <a href="https://music.163.com/#/download" target="_blank"></a>
      </div>

      <div class="banner-control">
        <button class="btn left" @click="bannerRef.prev()"></button>
        <button class="btn right" @click="bannerRef.next()"></button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted, ref } from 'vue';

import { useStore } from "@/store";

import { useState } from "@/hooks";

export default defineComponent({
  setup() {
    const store = useStore()
    const bannerRef = ref(null)
    let current = ref(0)
    
    // const banners = computed(() => store.state.recommend.topBanners)
    // const bgImage = computed(() => store.state.recommend.bgImage)

    const storeState = useState("recommend", ["topBanners", "bgImage"]);
    // const storeRootState = useState(["name", "age"]);
    // console.log("module:", storeState);
    // console.log("root:", storeRootState);
    
    onMounted(() => {
      store.dispatch("recommend/getTopBannersAction")
    });

    const bannerBeforeChange = (from: number, to: number) => {
      current.value = to;
      const imgUrl = storeState.topBanners.value[current.value] && storeState.topBanners.value[current.value].imageUrl
      store.commit("recommend/changeBgImage", imgUrl)
    }

    return {
      bannerRef,
      ...storeState,
      bannerBeforeChange
    }
  }
})
</script>

<style lang="less" scoped>
@import './index.less';
</style>